package cn.wolfcode.p2p.business.service.impl;

import java.math.RoundingMode;
import java.util.Date;

import cn.wolfcode.p2p.base.domain.LoginInfo;

import cn.wolfcode.p2p.business.domain.Bid;
import cn.wolfcode.p2p.business.domain.BidRequest;
import cn.wolfcode.p2p.business.domain.PaymentSchedule;
import cn.wolfcode.p2p.business.domain.PaymentScheduleDetail;
import cn.wolfcode.p2p.business.mapper.PaymentScheduleDetailMapper;
import cn.wolfcode.p2p.business.service.IPaymentScheduleDetailService;
import cn.wolfcode.p2p.util.CalculatetUtil;
import cn.wolfcode.p2p.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import sun.text.resources.cldr.ti.FormatData_ti;

import java.math.BigDecimal;
import java.util.List;

@Service
@Transactional
public class PaymentScheduleDetailServiceImpl implements IPaymentScheduleDetailService {
    @Autowired
    private PaymentScheduleDetailMapper detailMapper;

    @Override
    public void createPaymentScheduleDetail(PaymentSchedule ps, BidRequest br) {
        //根据一期的还款计划生成这一期的多个收款计划
        List<Bid> bids = br.getBids();
        BigDecimal totalInterest = Constants.ZERO;
        BigDecimal totalPrincipal = Constants.ZERO;

        for (int i = 0; i < bids.size(); i++) {
            Bid bid = bids.get(i);
            PaymentScheduleDetail detail = new PaymentScheduleDetail();
            detail.setBidAmount(bid.getAvailableAmount());
            detail.setBidId(bid.getId());
            detail.setBidRequestId(br.getId());
            detail.setMonthIndex(ps.getMonthIndex());
            detail.setDeadLine(ps.getDeadLine());
            detail.setReturnType(br.getReturnType());
            detail.setPaymentScheduleId(ps.getId());
            detail.setFromLoginInfo(br.getCreateUser());
            detail.setToLoginInfoId(bid.getBidUser().getId());
            /*
             * 如果是当前期的最后一个收款期, 最后一期的本金= 当前期的还款本金-非最后一盒收款期的本金之和, 利息一样
             */
            if (i == bids.size()-1) {
                detail.setPrincipal(ps.getPrincipal().subtract(totalPrincipal));
                detail.setInterest(ps.getInterest().subtract(totalInterest));
                detail.setTotalAmount(detail.getInterest().add(detail.getPrincipal()));

            } else {
                //收款本金 当前期的收款占当前期还款本金的多少
                // 投标金额/借款金额 * 当前期的还款本金
                BigDecimal monthlyReceivePrincipal = CalculatetUtil.calMonthlyReceivePrincipal(bid.getAvailableAmount(), br.getBidRequestAmount(), ps.getPrincipal());
                detail.setPrincipal(monthlyReceivePrincipal.setScale(Constants.STORE_SCALE, RoundingMode.HALF_UP));
                //收款利息
                BigDecimal monthlyReceiveInterest = CalculatetUtil.calMonthlyReceiveInterest(bid.getAvailableAmount(), br.getBidRequestAmount(), ps.getInterest());
                detail.setInterest(monthlyReceiveInterest.setScale(Constants.STORE_SCALE, RoundingMode.HALF_UP));
                //收款总额
                detail.setTotalAmount(detail.getInterest().add(detail.getPrincipal()));
                totalInterest = totalInterest.add(detail.getInterest());
                totalPrincipal = totalPrincipal.add(detail.getPrincipal());
            }
            detailMapper.insert(detail);
            ps.getPaymentScheduleDetails().add(detail);
        }
    }

    @Override
    public void update(PaymentScheduleDetail psd) {
         detailMapper.updateByPrimaryKey(psd);
    }

    @Override
    public void batchUpdateTransferState(Long[] bidIds, int transferState) {
        detailMapper.batchUpdateTransferState(bidIds,transferState);
    }
}
